package com.graduation.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.common.domain.dto.PageDTO;
import com.graduation.common.exceptions.BizException;
import com.graduation.mapper.CommentMapper;
import com.graduation.pojo.po.Case;
import com.graduation.pojo.po.Comment;
import com.graduation.pojo.po.LegalDocument;
import com.graduation.pojo.po.User;
import com.graduation.pojo.query.CommentQuery;
import com.graduation.pojo.query.FrontCommentQuery;
import com.graduation.pojo.vo.CommentVO;
import com.graduation.pojo.vo.FrontCommentVO;
import com.graduation.service.CaseService;
import com.graduation.service.CommentService;
import com.graduation.service.LegalDocumentService;
import com.graduation.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final UserService userService;

    private final LegalDocumentService legalDocumentService;

    private final CaseService caseService;

    @Override
    public PageDTO<CommentVO> pageComments(CommentQuery query) {
        // 使用自定义SQL进行联查
        Page<CommentVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<CommentVO> result = baseMapper.selectCommentVOPage(page, query);
        
        return PageDTO.of(result.getPages(), result.getTotal(), result.getRecords());
    }

    @Override
    public void saveComment(Comment comment) {
        // 验证用户是否存在
        User user = userService.getById(comment.getUserId());
        if (user == null) {
            throw new BizException("用户不存在");
        }

        // 验证关联的文档是否存在
        if (comment.getDocumentId() != null) {
            LegalDocument document = legalDocumentService.getById(comment.getDocumentId());
            if (document == null) {
                throw new BizException("关联的法律文档不存在");
            }
        }

        // 验证关联的案件是否存在
        if (comment.getCaseId() != null) {
            Case caseEntity = caseService.getById(comment.getCaseId());
            if (caseEntity == null) {
                throw new BizException("关联的案件不存在");
            }
        }

        // 验证父评论是否存在
        if (comment.getParentId() != null) {
            Comment parentComment = getById(comment.getParentId());
            if (parentComment == null) {
                throw new BizException("父评论不存在");
            }
        }
        
        save(comment);
    }

    @Override
    public void deleteComment(Long id) {
        Comment comment = getById(id);
        if (comment == null) {
            throw new BizException("评论不存在");
        }

        // 检查是否有子评论
        boolean hasChildren = lambdaQuery()
                .eq(Comment::getParentId, id)
                .exists();
        if (hasChildren) {
            throw new BizException("该评论下存在回复，无法删除");
        }
        
        removeById(id);
    }

    @Override
    public CommentVO getByCommentId(Long id) {
        CommentVO commentVO = baseMapper.selectCommentVOById(id);
        if (commentVO == null) {
            throw new BizException("评论不存在");
        }
        return commentVO;
    }

    @Override
    public PageDTO<FrontCommentVO> pageFrontComments(FrontCommentQuery query) {
        // 使用专门的前台查询SQL
        Page<FrontCommentVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<FrontCommentVO> result = baseMapper.selectFrontCommentVOPage(page, query);
        
        return PageDTO.of(result.getPages(), result.getTotal(), result.getRecords());
    }
}
